package com.gitee.melin.bee.core.jdbc.relational;

import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.DatabaseMetaData;

@Data
public class MetaDataSource {

    private static final Logger LOG = LoggerFactory.getLogger(MetaDataSource.class);

    private boolean readOnly;

    private String databaseProductName;

    private String databaseProductVersion;

    private String driverName;

    private String driverVersion;

    private DatabaseVersion databaseVersion;

    private boolean supportsBatchUpdates = false;

    private boolean supportsTransactions;

    private String compatibleMode; // oceanbean 等数据库，兼容模式：mysql，oracle

    public MetaDataSource() {
    }

    public MetaDataSource(DatabaseMetaData metaData, DatabaseVersion version) {
        try {
            this.readOnly = metaData.isReadOnly();
        } catch (Throwable e) {
            LOG.debug(e.getMessage());
            this.readOnly = false;
        }

        try {
            this.databaseProductName = metaData.getDatabaseProductName();
        } catch (Throwable e) {
            LOG.debug(e.getMessage());
            this.databaseProductName = "?"; //$NON-NLS-1$
        }
        try {
            this.databaseProductVersion = metaData.getDatabaseProductVersion();
        } catch (Throwable e) {
            LOG.debug(e.getMessage());
            this.databaseProductVersion = "?"; //$NON-NLS-1$
        }
        try {
            String name = metaData.getDriverName();
            if (name != null) {
                this.driverName = name;
            }
        } catch (Throwable e) {
            LOG.debug(e.getMessage());
            this.driverName = "?"; //$NON-NLS-1$
        }
        try {
            this.driverVersion = metaData.getDriverVersion();
        } catch (Throwable e) {
            LOG.debug(e.getMessage());
            this.driverVersion = "?"; //$NON-NLS-1$
        }

        try {
            supportsBatchUpdates = metaData.supportsBatchUpdates();
        } catch (Throwable e) {
            LOG.debug(e.getMessage());
        }

        try {
            supportsTransactions = metaData.supportsTransactions();
        } catch (Throwable e) {
            LOG.debug(e.getMessage());
            supportsTransactions = true;
        }

        this.databaseVersion = version;
    }

}
